package com.whf.android.jar.net;


import android.graphics.Bitmap;
import android.webkit.URLUtil;

import com.blankj.utilcode.util.ObjectUtils;
import com.whf.android.jar.app.Latte;
import com.whf.android.jar.net.callback.IError;
import com.whf.android.jar.net.callback.IFailure;
import com.whf.android.jar.net.callback.IRequest;
import com.whf.android.jar.net.callback.ISuccess;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 网络参数
 *
 * @author 傅令杰
 * @version 2017/4/2
 */
public final class RestClientBuilder {

    //加密等请求头
    private final WeakHashMap<String, Object> HEADERS = new WeakHashMap<>();

    /// 参数
    private final WeakHashMap<String, Object> PARAMS = new WeakHashMap<>();
    private String mUrl = null;
    private boolean mJson = false;
    private IRequest mRequest = null;
    private ISuccess mSuccess = null;
    private IFailure mFailure = null;
    private IError mError = null;
    private RequestBody mBody = null;
    private File mFile = null;
    private WeakHashMap<String, String> mFileSign = null;
    private List<File> mFileArray = null;
    private String mDownloadDir = null;
    private String mExtension = null;
    private String mName = null;

    RestClientBuilder() {
        //默认消息头
        HEADERS.put("Content-Type", "application/json;charset=utf-8");
    }

    public final RestClientBuilder url(String url) {
        if (Latte.isSwitchingServices()) {
            if (URLUtil.isNetworkUrl(url) || URLUtil.isAssetUrl(url)) {
                this.mUrl = Latte.getField() + url;
            } else {
                this.mUrl = Latte.getServer() + Latte.getField() + url;
            }
        } else {
            this.mUrl = Latte.getField() + url;
        }
        return this;
    }

    public final RestClientBuilder headers(WeakHashMap<String, Object> headers) {
        HEADERS.putAll(headers);
        return this;
    }

    public final RestClientBuilder headers(String key, Object value) {
        HEADERS.put(key, value);
        return this;
    }

    public final RestClientBuilder params(WeakHashMap<String, Object> params) {
        PARAMS.putAll(params);
        return this;
    }

    public final RestClientBuilder params(String key, Object value) {
        PARAMS.put(key, value);
        return this;
    }

    public final RestClientBuilder analysis(boolean json) {
        this.mJson = json;
        return this;
    }

    public final RestClientBuilder bitmap(Bitmap bitmap) {
        if (ObjectUtils.isEmpty(bitmap)) {
            return this;
        }
        // 将Bitmap转换为字节流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        byte[] bitmapBytes = byteArrayOutputStream.toByteArray();
        // 创建RequestBody
        this.mBody = RequestBody.create(MediaType.parse("image/jpeg"), bitmapBytes);
        return this;
    }

    public final RestClientBuilder file(File file) {
        this.mFile = file;
        return this;
    }

    public final RestClientBuilder fileSign(WeakHashMap<String, String> fileSign) {
        this.mFileSign = fileSign;
        return this;
    }

    public final RestClientBuilder file(String file) {
        this.mFile = new File(file);
        return this;
    }

    public final RestClientBuilder fileArray(File file) {
        mFileArray = new ArrayList<>();
        mFileArray.add(file);
        return this;
    }

    public final RestClientBuilder fileArray(String file) {
        mFileArray = new ArrayList<>();
        mFileArray.add(new File(file));
        return this;
    }

    public final RestClientBuilder fileList(List<File> fileArray) {
        this.mFileArray = fileArray;
        return this;
    }

    public final RestClientBuilder fileArray(List<String> fileArray) {
        mFileArray = new ArrayList<>();
        final int size = fileArray == null ? 0 : fileArray.size();
        for (int i = 0; i < size; i++) {
            final String file = fileArray.get(i);
            mFileArray.add(new File(file));
        }
        return this;
    }

    /**
     * 上传文件时设置文件名
     *
     * @param name 文件名
     */
    public final RestClientBuilder name(String name) {
        this.mName = name;
        return this;
    }

    /**
     * 下载文件时设置下载目录
     *
     * @param dir 文件目录
     */
    public final RestClientBuilder dir(String dir) {
        this.mDownloadDir = dir;
        return this;
    }

    /**
     * 下载文件时设置文件名
     *
     * @param extension 文件名后缀
     */
    public final RestClientBuilder extension(String extension) {
        this.mExtension = extension;
        return this;
    }

    /**
     * 上传文件等设置文件内容
     *
     * @param raw 文件等内容body
     */
    public final RestClientBuilder raw(String raw) {
        if (ObjectUtils.isEmpty(raw)) {
            return this;
        }
        this.mBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), raw);
        return this;
    }

    public final RestClientBuilder onRequest(IRequest iRequest) {
        this.mRequest = iRequest;
        return this;
    }

    public final RestClientBuilder success(ISuccess iSuccess) {
        this.mSuccess = iSuccess;
        return this;
    }

    public final RestClientBuilder failure(IFailure iFailure) {
        this.mFailure = iFailure;
        return this;
    }

    public final RestClientBuilder error(IError iError) {
        this.mError = iError;
        return this;
    }

    @NotNull
    @Contract(value = " -> new", pure = true)
    public final RestClient build() {
        return new RestClient(mUrl, mJson, PARAMS,
                mDownloadDir, mExtension, mName,
                mRequest, mSuccess, mFailure,
                mError, mBody, mFile, mFileArray,
                mFileSign, HEADERS);
    }
}

